﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace DataLibrary.Utility
{
   public  class WebHtmlHelper
    {
        public static string GetHtmlSource(string url, Encoding charset)

        {
            string _html = string.Empty;

            try

            {
                HttpWebRequest _request = (HttpWebRequest)WebRequest.Create(url);

                HttpWebResponse _response = (HttpWebResponse)_request.GetResponse();

                using (Stream _stream = _response.GetResponseStream())

                {
                    using (StreamReader _reader = new StreamReader(_stream, charset))

                    {
                        _html = _reader.ReadToEnd();

                    }

                }

            }

            catch (WebException ex)

            {
                using (StreamReader sr = new StreamReader(ex.Response.GetResponseStream()))

                {
                    _html = sr.ReadToEnd();

                }

            }

            catch (Exception ex)

            {
                _html = ex.Message;

            }

            return _html;

        }
    }
}
